package com.utils;

import java.util.HashMap;
import java.util.Iterator;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {
	private SoundPool soundPool;
	
	private HashMap<Integer, Integer> soundIds;
	private HashMap<Integer, Integer> streamIds;

	private HashMap<Integer, Boolean> soundStartedMap;
	private HashMap<Integer, Boolean> soundPlayingMap;
	private Context context;

	public SoundManager(Context context)
	{
		this.context = context;
		this.soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100);
		
		this.soundIds = new HashMap<Integer, Integer>();
		this.streamIds = new HashMap<Integer, Integer>();
		
		this.soundStartedMap = new HashMap<Integer, Boolean>();
		this.soundPlayingMap = new HashMap<Integer, Boolean>();	
	}
	
	public void unloadAllSounds()
	{
		//Unload existing sound
		Iterator<Integer> iter = soundIds.values().iterator();
		while(iter.hasNext())
		{
			soundPool.unload((Integer)iter.next());
		}
		soundIds.clear();
		streamIds.clear();
		
		soundStartedMap.clear();
		soundPlayingMap.clear();
	}

	public void addSound(int Index, int soundResource) {
	    soundIds.put(Index, soundPool.load(context, soundResource, 1));
	    soundStartedMap.put(Index, false);
	    soundPlayingMap.put(Index, false);
	}

	public void playSound(int index) {
		Boolean started = soundStartedMap.get(index);
		Boolean playing = soundPlayingMap.get(index);
		if(!started) {
			soundStartedMap.put(index, true);
			soundPlayingMap.put(index, true);
	    	streamIds.put(index, soundPool.play(soundIds.get(index), 0.9f, 0.9f, 1, -1, 1f));
		}
		else if(!playing) {
			soundPlayingMap.put(index, true);
			soundPool.resume(streamIds.get(index));
		}
	}
	
	public void pauseSound(int index) {
		Boolean started = soundStartedMap.get(index);
		Boolean playing = soundPlayingMap.get(index);
		if(started && playing) {
			soundPlayingMap.put(index, false);
			soundPool.pause(streamIds.get(index));
		}
	}
	
	public void playSoundAndStopOthers(int index)
	{
		stopAllPlayingSoundsExcept(index);
		playSound(index);
	}
	
	public void pauseAllPlayingSounds()
	{
		for(Integer index : soundStartedMap.keySet())
		{
			if(soundStartedMap.get(index) && soundPlayingMap.get(index))
			{
				soundPlayingMap.put(index, false);
			}
		}
		soundPool.autoPause();
	}
	
	public void stopAllPlayingSounds()
	{
		stopAllPlayingSoundsExcept(null);
	}
	
	private void stopAllPlayingSoundsExcept(Integer exception)
	{
		for(Integer index : soundStartedMap.keySet())
		{
			if((exception != null || index != exception) && soundStartedMap.get(index))
			{
				soundStartedMap.put(index, false);
				soundPlayingMap.put(index, false);
				soundPool.stop(streamIds.get(index));
			}
		}
	}
	
	public void stopSound(int index) {
		Boolean started = soundStartedMap.get(index);
		if(started) {
			soundStartedMap.put(index, false);
			soundPlayingMap.put(index, false);
			soundPool.stop(streamIds.get(index));
		}
	}
	
	public boolean isPlaying(int index) {
		if(soundPlayingMap.get(index)) {
			return true;
		}
		return false;
	}
	
	public void setSoundPropertiesPlaying(int index, float rate, float leftVol, float rightVol) {
		Boolean playing = soundPlayingMap.get(index);
		if(playing) {
			soundPool.setRate(streamIds.get(index), rate);
			soundPool.setVolume(streamIds.get(index), leftVol, rightVol);
		}
	}
	public void setSoundProperties(int index, float rate, float leftVol, float rightVol) {
		soundPool.setRate(streamIds.get(index), rate);
		soundPool.setVolume(streamIds.get(index), leftVol, rightVol);
	}
}